package com.kudian.restful.controller;

import com.kudian.common.exception.ApplicationException;
import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.DateUtils;
import com.kudian.common.utils.JsonUtils;
import com.kudian.common.utils.RegUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.dao.UsersDao;
import com.kudian.restful.entity.LoginSession;
import com.kudian.restful.entity.Seller;
import com.kudian.restful.entity.UserAddress;
import com.kudian.restful.entity.Users;
import com.kudian.restful.service.*;
import com.kudian.restful.vo.address.AddAddressVO;
import com.kudian.restful.vo.address.GetAddressesRetVO;
import com.kudian.restful.vo.address.GetAddressesVO;
import com.kudian.restful.vo.login.*;
import com.kudian.restful.vo.register.User4CustomerVO;
import com.kudian.restful.vo.register.UserVO;
import com.kudian.restful.vo.seller.QuerySellerVO;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/4/12.
 */
@RestController
@RequestMapping("user")
public class UserController {
    private static final Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UsersService usersService;

    @Autowired
    private LoginSessionService loginSessionService;

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private MediaResService mediaResService;

    @RequestMapping(value = "register4customer", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "注册客户端用户信息", httpMethod = "POST", notes = "用户信息以json格式传递", response = ResultObj.class)
    public @ResponseBody ResultObj register4customer(@RequestBody User4CustomerVO vo) {
        logger.info("register4customer=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        if (StringUtils.isBlank(vo.getNiceName())) {
            o.setErrcode(1001);
            o.setErrmsg("昵称不能为空");
            return o;
        }

        if (StringUtils.isBlank(vo.getPassword()) || StringUtils.isBlank(vo.getConfirmpassword())) {
            o.setErrcode(1002);
            o.setErrmsg("密码不能为空");
            return o;
        }
        if (!vo.getPassword().equals(vo.getConfirmpassword())) {
            o.setErrcode(1003);
            o.setErrmsg("密码与确认密码不一致");
            return o;
        }

        Users usersEntity = new Users();

        usersEntity.setEmail("");
        usersEntity.setUserName(vo.getNiceName());
        usersEntity.setMobilePhone(vo.getMobilePhone());

        usersEntity.setAlias("");
        usersEntity.setSex((short) 0);
        usersEntity.setIsValidated((short) 0);
        // 最大消费
        usersEntity.setCreditLine(0.0);

        // TODO
        usersEntity.setPassword(usersService.encrypt(vo.getPassword()));
        usersEntity.setPayPwd("");
        usersEntity.setUserMoney(0.0);
        usersEntity.setFrozenMoney(0.0);

        Timestamp t = new Timestamp(System.currentTimeMillis());
        usersEntity.setRegTime(t);
        usersEntity.setLastLogin(t);
        usersEntity.setLastTime(t);
        usersEntity.setLastIp("");
        // 不特殊
        usersEntity.setIsSpecial((short) 0);
        // TODO 生成随机数
        usersEntity.setSalt("abcdef");

        try {
            // 校验用户是否被注册过
            int r = usersService.checkRegisterUser(vo.getNiceName(), vo.getMobilePhone());
            if (!(r == 0 || r == 1 || r ==2)) {
                throw new Exception();
            }

            if (r == 1) {
                o.setErrcode(1004);
                o.setErrmsg("该手机号已被注册，请使用其他手机号注册");
                return o;
            } else if (r == 2) {
                o.setErrcode(1004);
                o.setErrmsg("该用户名已被注册，请使用其他用户名注册");
                return o;
            }

            usersService.saveUsers(usersEntity);
            o.setErrcode(0);
            o.setErrmsg("注册成功!");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("注册失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "register", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "注册用户信息", httpMethod = "POST", notes = "用户信息以json格式传递", response = ResultObj.class)
    public @ResponseBody ResultObj register(@RequestBody UserVO user) {
        logger.info("register=>" + JsonUtils.toJson(user));
        ResultObj o = new ResultObj();
        System.out.println(user.getName());

        if (StringUtils.isBlank(user.getName())) {
            o.setErrcode(1001);
            o.setErrmsg("注册名不能为空");
            return o;
        }

        if (StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(user.getConfirmpassword())) {
            o.setErrcode(1002);
            o.setErrmsg("密码不能为空");
            return o;
        }
        if (!user.getPassword().equals(user.getConfirmpassword())) {
            o.setErrcode(1003);
            o.setErrmsg("密码与确认密码不一致");
            return o;
        }
        Users usersEntity = new Users();

//        if (RegUtils.isEmail(user.getName())) {
//            usersEntity.setEmail(user.getName());
//            usersEntity.setMobilePhone("");
//            usersEntity.setUserName("");
//        } else
        if (RegUtils.isMobileNO(user.getName())) {
            usersEntity.setEmail("");
            usersEntity.setMobilePhone(user.getName());
            usersEntity.setUserName("");
        } else {
            usersEntity.setEmail("");
            usersEntity.setMobilePhone("");
            usersEntity.setUserName(user.getName());
        }
        usersEntity.setAlias("");
        usersEntity.setSex((short) 0);
        usersEntity.setIsValidated((short) 0);
        // 最大消费
        usersEntity.setCreditLine(0.0);

        usersEntity.setPassword(usersService.encrypt(user.getPassword()));
        usersEntity.setPayPwd("");
        usersEntity.setUserMoney(0.0);
        usersEntity.setFrozenMoney(0.0);

        Timestamp t = new Timestamp(System.currentTimeMillis());
        usersEntity.setRegTime(t);
        usersEntity.setLastLogin(t);
        usersEntity.setLastTime(t);
        usersEntity.setLastIp("");
        // 不特殊
        usersEntity.setIsSpecial((short) 0);
        // TODO 生成随机数
        usersEntity.setSalt("abcdef");

        try {
            // 校验此用户是否被注册
            Users userCk = usersService.getUser(user.getName());
            if (userCk != null) {
                o.setErrcode(1004);
                o.setErrmsg("已有此注册用户，请换其他账号注册");
                return o;
            }

            usersService.saveUsers(usersEntity);
            o.setErrcode(0);
            o.setErrmsg("注册成功!");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("注册失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "login", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "登录系统", httpMethod = "POST", notes = "登录信息用json格式传递,返回的access_token是后续使用,如果是商家版,增加SRegistrationId", response = LoginRetVO.class)
    public @ResponseBody LoginRetVO login(@RequestBody LoginVO loginvo) {
        logger.info("login=>" + JsonUtils.toJson(loginvo));
        LoginRetVO o = new LoginRetVO();
        try {
            String accessToken = usersService.login(loginvo);

            QuerySellerVO q = new QuerySellerVO();
            q.setAccess_token(accessToken);

            // 目前只有一个商家
            List<Seller> sellers = sellerService.query(q);
            if (sellers != null && !sellers.isEmpty()) {
                Seller s = sellers.get(0);

                // 取得绑定商家的状态
                o.setSellerStatus(s.getSellerStatus());
                //
                o.setWelcomeMsg(getWelcomMsg(s));
                if (s.getGtId() == 7) {
                    o.setSpe("1");
                } else {
                    o.setSpe("");
                }
            }

            o.setErrcode(0);
            o.setErrmsg("登录成功!");
            o.setAccess_token(accessToken);
        } catch (ApplicationException e) {
            // 失败
            o.setErrcode(e.getErrcode());
            o.setErrmsg(e.getErrmsg());
            o.setAccess_token("");
            logger.error(e);
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("登录失败!");
            o.setAccess_token("");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryLogin", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "快速登录", httpMethod = "POST", notes = "只使用,返回的access_token是后续使用,如果是商家版,增加SRegistrationId", response = LoginRetVO.class)
    public LoginRetVO queryLogin(@RequestBody QuickLoginVO vo) {
        logger.info("queryLogin=>" + JsonUtils.toJson(vo));
        LoginRetVO o = new LoginRetVO();
        try {
            String accessToken = usersService.quicklogin(vo);

            QuerySellerVO q = new QuerySellerVO();
            q.setAccess_token(accessToken);

            // 目前只有一个商家
            List<Seller> sellers = sellerService.query(q);
            if (sellers != null && !sellers.isEmpty()) {
                Seller s = sellers.get(0);

                // 取得绑定商家的状态
                o.setSellerStatus(s.getSellerStatus());
                //
                o.setWelcomeMsg(getWelcomMsg(s));
                if (s.getGtId() == 7) {
                    o.setSpe("1");
                } else {
                    o.setSpe("");
                }
            }

            o.setErrcode(0);
            o.setErrmsg("登录成功!");
            o.setAccess_token(accessToken);
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("登录失败!");
            o.setAccess_token("");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "checkToken", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "检查token是否失效", httpMethod = "POST", notes="", response = ResultObj.class)
    public @ResponseBody ResultObj checkToken(@RequestBody CheckTokenVO vo) {
        logger.info("checkToken=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();

        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        try {
            LoginSession s = loginSessionService.getInfo(vo.getAccess_token());

            if (s != null && StringUtils.isNotBlank(s.getAccessToken())) {
                o.setErrcode(0);
                o.setErrmsg("token有效!");
            } else {
                // 失败
                o.setErrcode(8888);
                o.setErrmsg("无效的token!");
            }
        } catch (Exception e) {
            // 失败
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "checkDefPwd", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "检查是否是默认密码", httpMethod = "POST", notes="检查是否是默认密码", response = CheckPwdRetVO.class)
    public @ResponseBody CheckPwdRetVO checkDefPwd(@RequestBody CheckPwdVO vo) {
        logger.info("checkDefPwd=>" + JsonUtils.toJson(vo));
        CheckPwdRetVO o = new CheckPwdRetVO();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }
        try {
            LoginSession s = loginSessionService.getInfo(vo.getAccess_token());
            Users u = usersService.getUserInfo(s.getUserId());
            if (usersService.encrypt("88888888").equals(u.getPassword())) {
                o.setErrcode(0);
                o.setErrmsg("默认密码");
                o.setDefPwd(true);
            } else {
                o.setErrcode(0);
                o.setErrmsg("非默认密码");
                o.setDefPwd(false);
            }
        } catch (Exception e) {
            // 失败
            o.setErrcode(8888);
            o.setErrmsg("检查是否是默认密码失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "modifyPwd", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改密码", httpMethod = "POST", notes="修改密码，旧密码为空时默认密码", response = ResultObj.class)
    public @ResponseBody ResultObj modifyPwd(@RequestBody ModifyPwdVO vo) {
        logger.info("modifyPwd=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }
        try {
            LoginSession s = loginSessionService.getInfo(vo.getAccess_token());
            Users u = usersService.getUserInfo(s.getUserId());
            if (StringUtils.isBlank(vo.getOldPwd())) {
                if (!usersService.encrypt("88888888").equals(u.getPassword())) {
                    o.setErrcode(1001);
                    o.setErrmsg("旧密码非默认密码，请提供旧密码!");
                    return o;
                }
            } else {
                if (!usersService.encrypt(vo.getOldPwd()).equals(u.getPassword())) {
                    o.setErrcode(1002);
                    o.setErrmsg("旧密码不正确，请重新输入!");
                    return o;
                }
            }

            u.setPassword(usersService.encrypt(vo.getNewPwd()));
            u.setLastTime(DateUtils.getSysTimestamp());
            usersService.save(u);
            o.setErrcode(0);
            o.setErrmsg("成功修改密码");
        } catch (Exception e) {
            // 失败
            o.setErrcode(8888);
            o.setErrmsg("修改密码失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "checkUsersName", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "校验用户名", httpMethod = "POST", notes="校验用户名,返回手机号，只用户直接修改", response = ResultObj.class)
    public @ResponseBody CheckUserNameRetVO checkUsersName(@RequestBody CheckUserNameVO vo) {
        logger.info("checkUsersName=>" + JsonUtils.toJson(vo));
        CheckUserNameRetVO o = new CheckUserNameRetVO();

        try {
            String mobile = usersService.checkUsersName(vo);
            o.setErrcode(0);
            o.setErrmsg("校验用户名成功");
            o.setMobile(mobile);
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("校验用户名失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "imModifyPwd", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "直接修改密码", httpMethod = "POST", notes="直接修改密码", response = ResultObj.class)
    public @ResponseBody ResultObj imModifyPwd(@RequestBody ImModifyPwdVO vo) {
        logger.info("imModifyPwd=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();

        try {
            this.usersService.imModifyPwd(vo);
            o.setErrcode(0);
            o.setErrmsg("直接修改密码成功");
        } catch (Exception e) {
            // 失败
            o.setErrcode(8888);
            o.setErrmsg("直接修改密码失败!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "addAddress", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "添加地址", httpMethod = "POST", notes = "添加地址", response = ResultObj.class)
    public @ResponseBody ResultObj addAddress(@RequestBody AddAddressVO vo) {
        logger.info("addAddress=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }
        try {
            LoginSession s = loginSessionService.getInfo(vo.getAccess_token());
            if (s != null && StringUtils.isNotBlank(s.getAccessToken())) {

                UserAddress ua = new UserAddress();
                ua.setUserId(s.getUserId());
                // 收件人姓名
                ua.setConsignee(vo.getConsignee());
                ua.setSex(vo.getSex());
                ua.setProvince(vo.getProvince());
                ua.setCity(vo.getCity());
                ua.setDistrict(vo.getDistrict());
                ua.setAddress(vo.getAddress());
                ua.setZipcode(vo.getZipcode());
                ua.setMobile(vo.getMobile());
                ua.setBestTime(vo.getBestTime());
                this.userAddressService.save(ua);

                o.setErrcode(0);
                o.setErrmsg("成功添加地址");
            } else {
                // 失败
                o.setErrcode(8888);
                o.setErrmsg("无效的token!");
            }
        } catch (Exception e) {
            // 失败
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "getAddresses", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "获取保存收货信息", httpMethod = "POST", notes = "获取保存收货信息", response = GetAddressesRetVO.class)
    public @ResponseBody GetAddressesRetVO getAddresses(@RequestBody GetAddressesVO vo) {
        logger.info("getAddresses=>" + JsonUtils.toJson(vo));
        GetAddressesRetVO o = new GetAddressesRetVO();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            o.setAddresses(userAddressService.getAddresses(ent.getUserId()));
            o.setErrcode(0);
            o.setErrmsg("成功修改密码");
        } catch (Exception e) {
            // 失败
            o.setErrcode(8888);
            o.setErrmsg("修改密码失败!");
            logger.error(e);
        }
        return o;
    }

    private String getWelcomMsg(Seller s) {
        String welcomMsg = "";
        String d = "";
        if (0 == s.getSellerStatus()) {
            // 3 天后
            d = DateUtils.getAfterDate(s.getRegTime().getTime(), 3 * 24 * 60 * 60 *1000L);
            welcomMsg = "您申请入驻的店铺正在进行审核中。\n";
            welcomMsg += "预计在" + d + "审核完毕\n";
            welcomMsg += "期待您的加入!";
        } else if (1 == s.getSellerStatus()) {
            // 审核中 3日后
            d = DateUtils.getAfterDate(s.getRegTime().getTime(), 3 * 24 * 60 * 60 *1000L);
            welcomMsg = "您申请入驻的店铺正在进行审核中。\n";
            welcomMsg += "预计在" + d + "审核完毕\n";
            welcomMsg += "期待您的加入!";
        } else if (2 == s.getSellerStatus()) {
            // 审核通过
            // s.getAuditTime();
            Timestamp tm = null;
            if (s.getAuditTime() == null) {
                tm = s.getModifyTime();
            } else {
                tm = s.getAuditTime();
            }
            // d = DateUtils.getAfterDate(tm.getTime(), 3 * 24 * 60 * 60 * 1000L);
            d = DateUtils.dateFormat(tm);
            d = d.substring(0, 10);
            welcomMsg = "您申请入驻的店铺已经于" + d + "审核通过很高兴你的加入！";
        }
        return welcomMsg;
    }


    @RequestMapping(value = "savePayPwd", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改支付密码", httpMethod = "POST", notes="修改支付密码", response = ResultObj.class)
    public @ResponseBody ResultObj savePayPwd(@RequestBody SavePayPwdVO vo) {
        logger.info("savePayPwd=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            Users u = usersService.get(ent.getUserId());
            u.setPayPwd(vo.getPayPwd());
            u.setLastTime(DateUtils.getSysTimestamp());
            this.usersService.save(u);
            o.setErrcode(0);
            o.setErrmsg("成功修改支付密码");
        } catch(Exception e){
            o.setErrcode(0);
            o.setErrmsg("修改支付密码失败");
            logger.error(e);
        }
        return o;
    }

    /**
     * 更新 头像
     * @param vo
     * @return
     */
    @RequestMapping(value = "updateHeadPic", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新头像", httpMethod = "POST", notes="更新头像", response = ResultObj.class)
    public @ResponseBody ResultObj updateHeadPic(@RequestBody UpdateHeadPicVO vo) {
        logger.info("updateHeadPic=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            Users u = usersService.get(ent.getUserId());
            u.setHeadPic(vo.getHeadPic());
            u.setLastTime(DateUtils.getSysTimestamp());
            this.usersService.save(u);
            o.setErrcode(0);
            o.setErrmsg("成功更新头像");
        } catch(Exception e){
            o.setErrcode(0);
            o.setErrmsg("更新头像失败");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "details", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "取得用户信息", httpMethod = "POST", notes="取得用户信息", response = ResultObj.class)
    public @ResponseBody UserDetailsRetVO details(@RequestBody DetailsVO vo) {
        logger.info("details=>" + JsonUtils.toJson(vo));
        UserDetailsRetVO o = new UserDetailsRetVO();
        if (vo == null || vo.getAccess_token() == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(8888);
            o.setErrmsg("无效的token!");
            return o;
        }
        LoginSession ent = loginSessionService.get(vo.getAccess_token());
        if (ent == null) {
            o.setErrcode(9999);
            o.setErrmsg("access_token已失效!");
            return o;
        }

        try {
            Users u = usersService.get(ent.getUserId());
            o.setUserId(u.getUserId());
            o.setMobilePhone(u.getMobilePhone());
            o.setSex(u.getSex());
            o.setUserName(u.getUserName());
            o.setUserMoney(u.getUserMoney());
            if (StringUtils.isBlank(u.getHeadPic())) {
                o.setHeadPic("");
            } else {
                o.setHeadPic(this.mediaResService.getResUrl(u.getHeadPic()));
            }
            o.setErrcode(0);
            o.setErrmsg("成功取得用户信息");
        } catch(Exception e){
            o.setErrcode(0);
            o.setErrmsg("取得用户信息失败");
            logger.error(e);
        }
        return o;
    }
}
